package com.zxw.flow.service.impl.toc;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zxw.common.core.base.constant.SysConstants;
import com.zxw.common.core.base.enums.EnumBeanNameEnum;
import com.zxw.common.core.base.enums.ResponseEnum;
import com.zxw.common.core.base.enums.toc.FileTypeEnum;
import com.zxw.common.core.base.res.GobalResponseInterface;
import com.zxw.common.core.exception.AppException;
import com.zxw.common.web.util.BeanUtils;
import com.zxw.common.web.util.JwtUtils;
import com.zxw.flow.domain.dto.toc.FilePathDTO;
import com.zxw.flow.domain.dto.toc.FlowPathTreeDTO;
import com.zxw.flow.domain.entity.toc.FilePath;
import com.zxw.flow.domain.vo.toc.FilePathVO;
import com.zxw.flow.domain.vo.toc.FlowJsonDataVO;
import com.zxw.flow.mapper.toc.FilePathMapper;
import com.zxw.flow.service.toc.FilePathService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxw.flow.service.toc.FlowJsonDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-08-19
 */
@Service
@RequiredArgsConstructor
public class FilePathServiceImpl extends ServiceImpl<FilePathMapper, FilePath> implements FilePathService {

    private final FilePathMapper filePathMapper;

    private final FlowJsonDataService flowJsonDataService;

    @Override
    public FilePathDTO get(Long id) {
        String userAccount = JwtUtils.getUserAccount();
        FilePath filePath = filePathMapper.selectOneByIdAndUserAccount(id, userAccount);
        FilePathDTO filePathDTO = new FilePathDTO();
        BeanUtil.copyProperties(filePath, filePathDTO);
        filePathDTO.setFileTypeMsg(BeanUtils.getMsgByCode(filePathDTO.getFileType(), EnumBeanNameEnum.FILE_TYPE));
        return filePathDTO;
    }

    @Override
    public List<FlowPathTreeDTO> getTree() {
        String userAccount = JwtUtils.getUserAccount();
        List<FilePath> filePathList = filePathMapper.selectByUserAccount(userAccount);
        List<FlowPathTreeDTO> flowPathTreeDTOList = this.assembleFlowPathTree(filePathList, 0l);
        return flowPathTreeDTOList;
    }

    /**
     * 组装文件树
     *
     * @param filePathList
     * @param pid
     * @return
     */
    private List<FlowPathTreeDTO> assembleFlowPathTree(List<FilePath> filePathList, Long pid) {
        List<FlowPathTreeDTO> flowPathTreeDTOList = new ArrayList<>();
        Optional.ofNullable(filePathList).ifPresent(filePaths -> filePaths.stream().filter(item -> item.getPid().equals(pid))
                .forEach(sonfilePath -> {
                    FlowPathTreeDTO flowPathTreeDTO = new FlowPathTreeDTO();
                    BeanUtil.copyProperties(sonfilePath, flowPathTreeDTO);
                    flowPathTreeDTO.setLabel(sonfilePath.getName());
                    List<FlowPathTreeDTO> sonFlowPathTreeDTOList = assembleFlowPathTree(filePathList, flowPathTreeDTO.getId());
                    flowPathTreeDTO.setChildren(sonFlowPathTreeDTOList);
                    flowPathTreeDTOList.add(flowPathTreeDTO);
                })
        );
        return flowPathTreeDTOList;
    }


    @Override
    public FilePath add(FilePathVO filePathVO) {
        if (StrUtil.isEmpty(filePathVO.getFileTypeMsg())) {
            throw new AppException(ResponseEnum.FILE_TYPE_BE_EE_EMPTY);
        }
        FilePath filePath = new FilePath();
        BeanUtil.copyProperties(filePathVO, filePath);
        String userAccount = JwtUtils.getUserAccount();
        filePath.setUserAccount(userAccount);
        filePath.setFileType(BeanUtils.getCodeByMsg(filePathVO.getFileTypeMsg(), EnumBeanNameEnum.FILE_TYPE));
        filePathMapper.insert(filePath);
        return filePathVO;
    }

    @Override
    public FilePath init(String userAccount) {
        FilePath filePath = new FilePath();
        filePath.setUserAccount(userAccount);
        filePath.setPid(0l);
        filePath.setName("根节点");
        filePath.setFileType(BeanUtils.getCodeByMsg(FileTypeEnum.FOLDER.getMsg(), EnumBeanNameEnum.FILE_TYPE));
        filePathMapper.insert(filePath);

        FlowJsonDataVO fv = new FlowJsonDataVO();
        fv.setUserAccount(userAccount);
        fv.setFilePathId(filePath.getId());
        fv.setJsonData(SysConstants.FLOW_SERVICE_DEF_JSON_DATA);
        flowJsonDataService.insert(fv);
        return filePath;
    }

    @Override
    public void update(FilePathVO filePathVO) {
        LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<FilePath>()
                .eq(FilePath::getId, filePathVO.getId())
                .eq(FilePath::getUserAccount, filePathVO.getUserAccount())
                .set(FilePath::getName, filePathVO.getName());
        this.update(updateWrapper);
    }

    @Override
    public void delete(Long id) {
        String userAccount = JwtUtils.getUserAccount();
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<FilePath>()
                .eq(FilePath::getId, id)
                .eq(FilePath::getUserAccount, userAccount);
        this.remove(queryWrapper);
        // 删除文件对应的数据
        flowJsonDataService.deleteByFilePathId(id);
    }
}
